package com.joe.keyboard;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public class KeyBoardView extends RecyclerView {

    public KeyBoardView(@NonNull Context context) {
        super(context);
        init();
    }

    public KeyBoardView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public KeyBoardView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public void init() {
        for (int i = 1; i < 10; i++) {
            list.add(i + "");
        }

        setLayoutManager(new GridLayoutManager(getContext(), 3));
        addItemDecoration(new ItemDecoration() {

            @Override
            public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
                super.onDraw(c, parent, state);
            }

            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.top = 10;
                outRect.left = 10;
                outRect.right = 10;
            }
        });
        setAdapter(new KeyBoardAdapter());
        setPadding(10, 10, 10, 20);
        setBackgroundResource(R.drawable.keyboard_ry_bg);
    }

    private int keyType = 0;

    public void updateType(int keyType) {
        if (keyType == 0) {
            for (int i = 1; i < 10; i++) {
                list.add(i + "");
            }
        } else {
            String az = "ABCDEFGHIJKLMNPQRSTUVWXYZ";
            for (int i = 1; i < az.length(); i++) {
                list.add(az.substring(i, i + 1));
            }
        }
        invalidate();
    }

    private KeyItemClickListener keyItemClickListener;

    public void addClickListener(KeyItemClickListener keyItemClickListener) {
        this.keyItemClickListener = keyItemClickListener;
    }

    private List<String> list = new ArrayList<>();


    class KeyBoardAdapter extends Adapter<ViewHolder> {

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View ry = LayoutInflater.from(getContext()).inflate(R.layout.layout_keyboard_ly, null, false);
            return new ViewHolder(ry);
        }

        @Override
        public void onBindViewHolder(final @NonNull ViewHolder holder, final int position) {
            if (keyType == 0) {
                int type = 0;
                if (position < 9) {
                    type = 0;
                    holder.tv.setText(list.get(position) + "");
                    holder.tv.setVisibility(VISIBLE);
                    holder.img.setVisibility(GONE);
                } else if (position == 9) {
                    type = 1;
                    holder.tv.setText("-");
                    holder.tv.setVisibility(VISIBLE);
                    holder.img.setVisibility(GONE);
                } else if (position == 10) {
                    type = 2;
                    holder.tv.setText("0");
                    holder.tv.setVisibility(VISIBLE);
                    holder.img.setVisibility(GONE);
                } else {
                    type = 3;
                    holder.tv.setVisibility(GONE);
                    holder.img.setVisibility(VISIBLE);
                }

                holder.view.setTag(type);

                holder.view.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (keyItemClickListener != null) {
                            keyItemClickListener.onItemClick((int) v.getTag(), holder.tv.getText().toString());
                        }
                    }
                });
            } else {
                holder.tv.setText(list.get(position));
                holder.tv.setVisibility(VISIBLE);
                holder.img.setVisibility(GONE);

                holder.view.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (keyItemClickListener != null) {
                            keyItemClickListener.onItemClick(0, holder.tv.getText().toString());
                        }
                    }
                });
            }


        }

        @Override
        public int getItemCount() {
            return keyType == 0 ? list.size() + 3 : list.size();
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private View view;
        private TextView tv;
        private ImageView img;

        public ViewHolder(View v) {
            super(v);
            this.view = v;
            this.tv = v.findViewById(R.id.layout_keyboard_tv);
            this.img = v.findViewById(R.id.layout_keyboard_img);
        }
    }
}
